package com.fengkai.filepost.pdfoutstream.test.ODsolutions;


import java.util.LinkedList;
import java.util.Scanner;

/**
 * @author Fengkai
 * @creat 2023/3/29
 */
public class WaterNum {

    //判断是否水仙数
    public static Boolean isWaterNum (int number) {

        String s = String.valueOf(number);
        int n = s.length();
        int sum = 0;
        for (int i = 0; i < s.length(); i++) {
            sum += Math.pow(Integer.parseInt(String.valueOf(s.charAt(0))), n);
        }
        return number == sum;
    }

    //返回字符串ascii值
    public static int getASCIIString (String string) {
        int result = 0;
        for (int i = 0; i < string.length(); i++) {
            result += string.charAt(i);
        }
        return result;
    }

    public static void findStr(String string, LinkedList<Integer> answer , Integer count) {
        String str1;
        String str2;
        for (int i = 1; i < string.length(); i++) {
            str1 = string.substring(0, i);
            str2 = string.substring(i);
            if (isWaterNum(getASCIIString(str1))){
                if (isWaterNum(getASCIIString(str2))){
                    answer.add(count);
                }else {
                    findStr(str2, answer, count ++);
                }
            }
        }
    }

    public static void main (String[] args) {
        LinkedList<Integer> integers = new LinkedList<>();
        int count = 2;
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        findStr(s, integers, count);
        if (integers.size() > 1){
            System.out.println("-1");
        }else if (integers.size() == 0){
            System.out.println(0);
        }else {
            System.out.println(integers.get(0));
        }

    }


}
